/**
 * Created by Administrator on 2016/10/16.
 */
angular.module('userModule', [])
    .controller('userRegisterController', ['$userService', '$stateParams', '$scope','$state',
        function ($userService, $stateParams, $scope, $state) {
            $scope.user = {};
            /*
             user.email
             user.phoneNumber
             user.password
             user.confirmPassword
             */
            $scope.checkResult = {
                isValid: false,
                validMessage: '',
                validType:''
            }

            $scope.submitStatus = 0;
            $scope.submitMessage = "";

            $scope.resetSubmitStatus = function () {
                $scope.submitStatus = 0;
                $scope.submitMessage = "";
            }

            $scope.register = function () {
                $scope.checkRegisterInfo();

                if ($scope.checkResult.isValid) {
                    //TODO
                    $userService.registerUser($scope.user,function(data){
                        if(data && data.data){
                            var resultData = data.data;
                            if(resultData.result){
                                $scope.submitStatus = 1;
                                $scope.submitMessage = "账户 "+$scope.user.email+" 注册成功!";

                            }
                            else {
                                $scope.submitStatus = 2;
                                $scope.submitMessage = "注册失败,"+resultData.message;
                            }

                        }else {
                            $scope.submitStatus = 2;
                            $scope.submitMessage = "注册失败,请求超时";
                        }
                    },function(error){
                        $scope.submitStatus = 2;
                        $scope.submitMessage = "注册失败,数据库访问失败";
                    })

                }
            }

            $scope.checkRegisterInfo = function () {
                $scope.checkResult = {
                    isValid: false,
                    validMessage: '',
                    validType:''
                }

                var isValid = verifyEmail($scope.user.email)
                if (!isValid) {
                    $scope.checkResult.isValid = isValid;
                    $scope.checkResult.validMessage = "邮箱格式不正确";
                    $scope.checkResult.validType = 'email';
                    return;
                }

                if (!$scope.user.password) {
                    $scope.checkResult.isValid = false;
                    $scope.checkResult.validMessage = "请输入密码";
                    $scope.checkResult.validType = 'password';
                    return;
                }

                if ($scope.user.password.length  < 6) {
                    $scope.checkResult.isValid = false;
                    $scope.checkResult.validMessage = "密码长度小于6";
                    $scope.checkResult.validType = 'password';
                    return;
                }

                if ($scope.user.password != $scope.user.confirmPassword) {
                    $scope.checkResult.isValid = false;
                    $scope.checkResult.validMessage = "密码输入不一致";
                    $scope.checkResult.validType = 'confirmPassword';
                    return;
                }

                var isValid = verifyPhoneNum($scope.user.phoneNumber);
                if (!isValid) {
                    $scope.checkResult.isValid = isValid;
                    $scope.checkResult.validMessage = "手机格式不正确";
                    $scope.checkResult.validType = 'phoneNumber';
                    return;
                }

                $scope.checkResult.isValid = true;

            }

        }])
    .controller('resetPasswordByEmailCtrl', ['$userService', '$stateParams', '$scope','$location',
        function ($userService, $stateParams, $scope,$location) {
            $scope.submitBtnStr = "发送邮件";
            $scope.submitClickable = true;
            $scope.wait = 60;

            $scope.user = {};
            $scope.checkResult = {
                isValid:false,
                validMessage : '',
                validType : ''
            }

            $scope.sendEmail = function () {
                if(!$scope.submitClickable){
                    return ;
                }

                $scope.registerSuccess = false;
                $scope.checkEmail();

                if($scope.checkResult.isValid){

                    var params = {email:$scope.user.email};
                    $userService.sendEmail(params,function(data){
                        if(data && data.data){
                            var resultData = data.data;
                            if(resultData.result){
                                $scope.downCount();
                            }
                            else {
                                $scope.checkResult = {
                                    isValid:false,
                                    validType:'submit',
                                    validMessage : resultData.message
                                }
                                alert(resultData.message);
                            }

                        }else{
                            $scope.checkResult = {
                                isValid:false,
                                validType:'submit',
                                validMessage : '请求失败,网络异常'
                            }
                            alert('请求失败,网络异常');

                        }

                    },function(err){
                        $scope.checkResult = {
                            isValid:false,
                            validType:'submit',
                            validMessage : '网络异常'
                        }
                        alert('请求失败,网络异常1');
                    });

                }
            }

            $scope.checkEmail = function () {
                $scope.checkResult = {
                    isValid: false,
                    validMessage: '',
                    validType: ''
                }
                if (!$scope.user.email) {
                    $scope.checkResult.isValid = false;
                    $scope.checkResult.validMessage = "邮箱不能为空";
                    $scope.checkResult.validType = 'email';
                    return ;
                }

                var isValid = verifyEmail($scope.user.email);
                if (!isValid) {
                    $scope.checkResult.isValid = isValid;
                    $scope.checkResult.validMessage = "请输入正确的邮箱格式";
                    $scope.checkResult.validType = 'email';
                    return;
                }
                $scope.checkResult.isValid = true;

            };

            $scope.downCount = function () {
                if ($scope.wait == 0) {
                    $scope.submitClickable = true;
                    $scope.submitBtnStr = "发送邮件";
                    $scope.wait = 60;

                }
                else {
                    $scope.submitClickable = false;
                    $scope.wait = $scope.wait - 1;
                    $scope.submitBtnStr = "邮件已发送,请查收(" + $scope.wait + ")";

                    setTimeout(function () {
                        $scope.$apply($scope.downCount())
                    }, 1000);
                }
            };

        }])
    .controller('setNewPasswordByEmailCtrl', ['$userService', '$stateParams', '$scope','$state','$location',
        function ($userService, $stateParams, $scope, $state,$location) {
            $scope.params = $location.search();
            if(!$scope.params || !$scope.params.email || !$scope.params.authToken){
                $scope.submitStatus = 2;
                $scope.submitMessage = "参数不完整,无效的请求,请重新发送邮件找回";

                return ;
            }
            $scope.submitStatus = 0;

            $scope.user = {};
            /*
             user.password
             user.confirmPassword
             */
            $scope.checkResult = {
                isValid: false,
                validMessage: '',
                validType:''
            }

            $scope.updatePassword = function () {
                if($scope.submitStatus == 2){
                    return ;
                }

                $scope.checkPassword();

                if ($scope.checkResult.isValid) {
                    var params = {
                        email: $scope.params.email,
                        password: $scope.user.password,
                        authToken: $scope.params.authToken
                    }

                    $userService.updatePasswordByEmail(params,function(data){
                        if(data && data.data){
                            var resultData = data.data;
                            if(resultData.result){
                                $scope.submitStatus = 1;
                                $scope.submitMessage = "修改密码成功!";
                            }
                            else {
                                $scope.submitStatus = 2;
                                $scope.submitMessage = "修改密码失败:"+resultData.message;
                            }

                        }else {
                            $scope.submitStatus = 2;
                            $scope.submitMessage = "修改密码失败,请求超时";
                        }
                    },function(error){
                        $scope.submitStatus = 2;
                        $scope.submitMessage = "修改密码失败,请求超时";
                    })

                }
            }

            $scope.checkPassword = function () {
                $scope.checkResult = {
                    isValid: false,
                    validMessage: '',
                    validType:''
                }

                if (!$scope.user.password) {
                    $scope.checkResult.isValid = false;
                    $scope.checkResult.validMessage = "请输入密码";
                    $scope.checkResult.validType = 'password';
                    return;
                }

                if ($scope.user.password.length  < 6) {
                    $scope.checkResult.isValid = false;
                    $scope.checkResult.validMessage = "密码长度小于6";
                    $scope.checkResult.validType = 'password';
                    return;
                }

                if ($scope.user.password != $scope.user.confirmPassword) {
                    $scope.checkResult.isValid = false;
                    $scope.checkResult.validMessage = "密码输入不一致";
                    $scope.checkResult.validType = 'confirmPassword';
                    return;
                }

                $scope.checkResult.isValid = true;

            }

        }])
    .service('$userService', ['$http', function ($http) {
        /**
         * 用户注册
         */
        this.registerUser = function (params, success, fail) {
            $http({
                method: 'POST',
                url: '/users/',
                data: params
            }).then(success, fail);
        };
        /**
         * 用户登录
         */
        this.login = function (params, success, fail) {
            $http({
                method: 'POST',
                url: '/users/login/',
                data: params
            }).then(success, fail);
        }
        /*退出*/
        this.logout= function() {
            $http.post('/users/logout/');
        }
        /**
         * 发送邮件
         */
        this.sendEmail = function (params, success, fail) {
            $http({
                method: 'POST',
                url: '/resetPassword/email/',
                data: params
            }).then(success, fail);
        };
        /**
         * 通过邮箱重置密码
         */
        this.updatePasswordByEmail = function (params, success, fail) {
            $http({
                method: 'POST',
                url: '/resetPassword/',
                data: params
            }).then(success, fail);
        }
    }]);
